/**
 * Baijiahulian.com Inc. Copyright (c) 2014-2014 All Rights Reserved.
 */
package com.baijia.authentication.principal.resolver;

import com.baijia.authentication.BaijiaAccount;
import com.baijia.authentication.principal.Credentials;
import com.baijia.authentication.principal.CredentialsToPrincipalResolver;
import com.baijia.authentication.principal.Principal;
import com.baijia.authentication.principal.SimplePrincipal;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;


/**
 * @title AccountTypeCredentialsToPrincipalResolver
 * @desc 填充身份数据 
 * @author caoliang
 * @date 2015年12月5日
 * @version 1.0
 */
public abstract class AccountTypeCredentialsToPrincipalResolver
    implements CredentialsToPrincipalResolver, BaijiaAccount {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    
    protected static final String ACCOUNT_ID = "accountId";

    protected abstract String extractPrincipalId(final Credentials credentials); 
    
    protected abstract Map<String, Object> generateAttributes(final Credentials credentials);
    

    @Override
    public Principal resolvePrincipal(Credentials credentials) {
        logger.debug("Attempting to resolve a principal...");

        final String principalId = extractPrincipalId(credentials);

        if (principalId == null) {
            return null;
        }
        
         final Map<String, Object> convertedAttributes = generateAttributes(credentials);

        logger.debug("Creating SimplePrincipal for [" + principalId + "]");

        return new SimplePrincipal(principalId, convertedAttributes);
    }

}
